#include <iostream>
#include <string>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/syscall.h>
#include <pthread.h>

using namespace std;

__thread int g_val = 1000;//声明线程局部储存，代表着线程内有数据副本
__thread pid_t lwp = 0;

pid_t gettid(){
    return syscall(SYS_gettid);
}

void *rountine(void *args)
{
    std::string  name = static_cast<const char*>(args);
    lwp = gettid();
    while(true)
    {
         sleep(1);
        std::cout << name << ", g_val: " << g_val << " ,&g_val: " << &g_val << "\n"
                  << std::endl;
        std::cout <<"new thread: " << lwp << std::endl;
        g_val++;
        //diary~
    }
    return nullptr;
}

int main()
{
    pthread_t pid;
    pthread_create(&pid, NULL, rountine, (void*)"thread1");
    lwp = gettid();
    std::cout << "main thread: " << lwp << std::endl;

    while(1)
    {
        sleep(1);
        std::cout << "main thread, g_val: " << g_val << " ,&g_val: " << &g_val << "\n"
                  << std::endl;
    }
    pthread_join(pid, NULL);
    return 0;
}